<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Grid\ExamineCourseComment;
use App\Admin\Repositories\CourseComment;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class CourseCommentController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new CourseComment(['member', 'course']), function (Grid $grid) {
            $grid->model()->orderByDesc('id');

            $grid->column('id');
            $grid->column('member_id');
            $grid->column('member.nickname', '用户名称');
            $grid->column('course_id');
            $grid->column('course.name', '课程名称');
            $grid->column('content');
            $grid->column('status', '状态')->using(['未审核', '已通过', '未通过'])->label([
                'primary', 'success', 'danger',
            ]);
            $grid->column('created_at');

            $grid->actions(function (Grid\Displayers\Actions $actions) {
                if ($actions->row->status == 0) {
                    $actions->append(new ExamineCourseComment(\App\Models\CourseComment::class));
                }
            });

            $grid->disableFilterButton();
            $grid->disableRefreshButton();
            $grid->disableBatchDelete();
            $grid->disableRowSelector();
            $grid->disableCreateButton();
            $grid->disableViewButton();
            $grid->disableEditButton();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->expand();

                $filter->equal('id')->width(3);
                $filter->like('member.name', '用户名称')->width(3);
                $filter->like('course.name', '课程名称')->width(3);
                $filter->equal('status', '状态')->select(['未审核', '已通过', '未通过'])->width(3);
                $filter->between('created_at')->datetime()->width(3);
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new CourseComment(), function (Show $show) {
            $show->field('id');
            $show->field('member_id');
            $show->field('course_id');
            $show->field('content');
            $show->field('status');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new CourseComment(), function (Form $form) {
            $form->display('id');
            $form->text('member_id');
            $form->text('course_id');
            $form->text('content');
            $form->text('status');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
